<?php session_start();
if(!isset($_SESSION['login']))
{
    $_SESSION['login'] = false;
    $_SESSION['username'] = "dummy";
}

?>
<html>
    <head>
        <title>Daftar Konten</title>

    <link rel="stylesheet" type="text/css" title="T1" href="stylesheet/progin.css" id="link_theme" />
	<link rel="stylesheet" type="text/css" title="T1" href="stylesheet/posting.css" />
		<meta name="description" content="RERGag, Make You Laughing" />
		<meta name="keywords" content="laugh" />
		<meta name="author" content="Robertus Theodore" />
		<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
                <script type="text/javascript" src="script/home.js"></script>
                
    </head>
    <body>
        <div class="wrapper">
            <div class="header">
                <a href="index.htm" id="image"><img src="images/logo.png" alt="title" height="50"/></a>
                <h1>RERGAG - Just For Laugh</h1>
            </div>
            <div class="links">
                <ul>
                    <li><a href="registrasi.php" id="registrasi_link" >Registrasi</a></li>
                    <li><a href="index.php" id="home_link">Home</a></li>
                    <li><a href="register.php" id="daftar_link">Daftar</a></li>
                    <li><a href="posting.php" id="upload_link" class="active">Posting</a></li>
                </ul>
            </div>
        </div>

        <div>
            <form action="konten.php" name="type_tag" method="POST">
                    Filter<input type ="text" name="filter" value=""/>
                    Sort by : <select name="sort">
                        <option value="popular">Most Popular</option>
                        <option value="comment">Most Commented</option>
                        <option value="new" selected="selected">Newest</option>
                    </select>
                    <input type="submit" value="Select"/>
            </form>
            <?php
            $username = "root";
                $password = "";
                $database = "progin_171_13509008";
                mysql_connect('localhost',$username,$password);
                @mysql_select_db($database) or die("Unable to select database");
                $query = "SELECT * from tag";
                $result = mysql_query($query);
                echo ("<ul>");
                while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
                        echo ( "<li><a id=\"lol\" href=\"konten.php?tagname=".($row['tag_name'])." \" > ". ($row['tag_name']) ." </a></li>");
                }
                echo ("</ul>");
            ?>
        </div>
        <div class="main_container" id="main_content">
            
            <?php

            global $contentCount;

            function displayContent($row,$result){

                $number = 6;
               $page = 0;

                for ($i=0; $i < $number*$page; $i++)
                    mysql_fetch_array($result);
                
                for ($i = 0; $i < $number; $i++){
                    $row= mysql_fetch_array($result);
                    if($row['content_type']== 'link'){
                        echo "<br/><h1><a href='detail.php'>".$row['title']."</a></h1>";
                        echo "<h2>".$row['content_filled']."</h2>";
                        echo "<h3>".$row['content_description']."</h3>";
                        echo "<h3>Posted by:".$row['user_name']."</h3><br/>";
                    }
                    if($row['content_type']== 'image'){
                        echo "<br/><h1><a href='detail.php'>".$row['title']."</a></h1>";
                        echo "<img src='".$row['content_filled']."' height=500px; ></img>";
                        echo "<h3>Posted by: <a href='detail.php'>".$row['user_name']."</a></h3><br/>";
                    }
                    if($row['content_type']== 'video'){
                        echo "<br/><h1><a href='detail.php'>".$row['title']."</a></h1>";
                        echo $row['content_filled'];
                        echo "<h3>Posted by: <a href='detail.php'>".$row['user_name']."</a></h3><br/>";
                    }
                
                 
                   echo"<input id='like_".$row['id_konten']."' class='like_button' type='image' src='images/like_button.jpg' onclick='like(\"".$_SESSION['user_name']."\",".$row['id_konten'].")  height=30px;/>";
                   echo"<input class='total_like_class' type='text' disabled='disabled' id='total_like_".$row['id_konten']."' />";
                   echo"<input id='dislike_'".$row['id_konten']." class='like_button' type='image' src='images/dislike_button.jpg' onclick='dislike(\"".$_SESSION['user_name']."\",".$row['id_konten'].")'/>";
                   echo"<input class='total_dislike_class' type='text' disabled='disabled' id='total_dislike_".$row['id_konten']."' />";

                      echo"<script type='text/javascript'>"; //hitung nilai like dan dislike dulu!
                   echo "likeUpdater(".$row['id_konten']."); dislikeUpdater(".$row['id_konten'].");";
                   echo "</script>";
                   
                }
            }
                
                if(!isset($_POST['sort'])){
                   $sort = "new" ;
                }else{
                    $sort = $_POST['sort'];
                }
                if(!isset($_POST['filter'])){
                    $filter = "";
                }else{
                    $filter = $_POST['filter'];
                }
                if(!isset($_GET['tagname'])){
                    $getlink = "";
                }else{
                    $getlink = $_GET['tagname'];
                }
                $words = explode(",", $filter);
                if($getlink == ""){
                    if($words[0] == ""){
                        if($sort == "popular"){
                            $query = "Select id_content as id_konten, user_name, content.title, content.content_type, content.content_filled, content.posting_time from
                                        (Select c.id_content as e from ((Select b.id_content from (select id_content,count(*) as result from like_content where status = 1 group by id_content order by result desc) as b)
                                        union distinct
                                        (select not_like_result.id_content as not_like_result_id_content from (select id_content from like_content where status = 0 or status = 2 )as not_like_result)) as c) as most_like_order_id_content
                                        , content
                                where most_like_order_id_content.e = id_content";
                        }else{
                            if($sort == "comment"){
                                $query = "Select user_name, content.id_content as id_konten, content.title, content.content_type, content.content_filled, content.posting_time from content inner join (select most_comment.id_content as id_most_commented from (select id_content, count(*) as result from comment group by id_content order by result Desc) as most_comment)  comment_result
                                        on content.id_content = comment_result.id_most_commented";
                            }else{
                                $query = "select distinct user_name, content.id_content as id_konten, content.title, content.content_type, content.content_filled from
                                        (select id_content, TIMESTAMPDIFF(SECOND, posting_time, now()) as time from content order by time) time_order, content
                                        order by time_order.time";
                            }
                        }
                        $result = mysql_query($query);
                        $row = mysql_num_rows($result);
                        if($row == 0){
                            echo "No tag exists in database";
                        }else{
                            displayContent($row,$result);
                            while ($countRow = mysql_fetch_array($result)){
                                $contentCount++;
                            }
                        }
                    }else{
                        for($i = 0 ; $i < count($words) ; $i++){
                            $query = "CREATE VIEW upil AS SELECT * FROM content WHERE id_content = (SELECT id_content FROM tag_content WHERE id_tag =
                            (SELECT id_tag FROM tag WHERE tag_name = '".$words[$i]."'))";
                            $result = mysql_query($query);
                            if($sort == "popular"){
                                $query = "Select upil.user_name, upil.title, upil.content_type, upil.content_filled, upil.posting_time from
                                        (Select c.id_content as e from ((Select b.id_content from (select id_content,count(*) as result from like_content where status = 1 group by id_content order by result desc) as b)
                                        union distinct
                                        (select not_like_result.id_content as not_like_result_id_content from (select id_content from like_content where status = 0 or status = 2 )as not_like_result)) as c) as most_like_order_id_content
                                        , upil
                                where most_like_order_id_content.e = id_content";
                            }else{
                                if($sort == "comment"){
                                    $query = "Select upil.user_name, upil.title, upil.content_type, upil.content_filled, upil.posting_time from upil inner join (select most_comment.id_content as id_most_commented from (select id_content, count(*) as result from comment group by id_content order by result Desc) as most_comment)  comment_result
                                        on upil.id_content = comment_result.id_most_commented";
                                }else{
                                    $query = "select distinct upil.user_name, upil.title, upil.content_type, upil.content_filled from 
                                        (select id_content, TIMESTAMPDIFF(SECOND, posting_time, now()) as time from upil order by time) time_order, upil 
                                        order by time_order.time";
                                }
                            }
                            $result = mysql_query($query);
                            $row = mysql_num_rows($result);
                            $query2 = "drop VIEW upil";
                            $result2 = mysql_query($query2);
                            if($row == 0){
                                if($words[$i] == "" || $words[$i] == " "){

                                }else{
                                    echo "No tag exists in database";
                                }
                            }else{
                                displayContent($row,$result);
                                while ($countRow = mysql_fetch_array($result)){
                                    $contentCount++;
                                }
                            }
                        }
                    }
                }else{
                
                    //link tag
                    $query = "CREATE VIEW upil AS SELECT * FROM content WHERE id_content = (SELECT id_content FROM tag_content WHERE id_tag =
                    (SELECT id_tag FROM tag WHERE tag_name = '".$getlink."'))";
                    $result = mysql_query($query);
                    if($sort == "popular"){
                        $query = "Select upil.user_name, upil.title, upil.content_type, upil.content_filled, upil.posting_time from
                                (Select c.id_content as e from ((Select b.id_content from (select id_content,count(*) as result from like_content where status = 1 group by id_content order by result desc) as b)
                                union distinct
                                (select not_like_result.id_content as not_like_result_id_content from (select id_content from like_content where status = 0 or status = 2 )as not_like_result)) as c) as most_like_order_id_content
                                , upil
                        where most_like_order_id_content.e = id_content";
                    }else{
                        if($sort == "comment"){
                            $query = "Select upil.user_name, upil.title, upil.content_type, upil.content_filled, upil.posting_time from upil inner join (select most_comment.id_content as id_most_commented from (select id_content, count(*) as result from comment group by id_content order by result Desc) as most_comment)  comment_result
                                on upil.id_content = comment_result.id_most_commented";
                        }else{
                            $query = "select distinct upil.user_name, upil.title, upil.content_type, upil.content_filled from 
                                (select id_content, TIMESTAMPDIFF(SECOND, posting_time, now()) as time from upil order by time) time_order, upil 
                                order by time_order.time";
                        }
                    }
                    $result = mysql_query($query);
                    $row = mysql_num_rows($result);
                    displayContent($row,$result);

                // count page
                while ($countRow = mysql_fetch_array($result)){
                    $contentCount++;
                }
                $query2 = "drop VIEW upil";
                $result2 = mysql_query($query2);
                }
                global $totalPage;
                $totalPage = ceil($contentCount/6);
                while ($totalPage >0) {
                    echo "<button type='button' onclick='changePage(".$totalPage.")' style='float: right;'>".$totalPage."</button>";
                    $totalPage--;
                }
                mysql_close();
                /*
                * To change this template, choose Tools | Templates
                * and open the template in the editor.
                */
            ?>
            
        </div>
        <div class="sidebar_container">
            
        </div>
    </body>
</html>


